<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <img src="../assets/logo.png" alt="中国铁塔" class="logo">
        <h1>铁塔财务部智能化报销管控平台</h1>
      </div>
      <div class="header-right">
        <span class="welcome-text">欢迎您，{{ userInfo.name }}</span>
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <i class="el-icon-user"></i>
            {{ userInfo.name }}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userInfo">个人信息</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="230px">
        <el-menu
          :default-active="activePath"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          unique-opened
          router>
          
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-user-solid"></i>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/user">
              <i class="el-icon-s-custom"></i>
              <span>系统用户管理</span>
            </el-menu-item>
            <el-menu-item index="/role">
              <i class="el-icon-s-check"></i>
              <span>角色管理</span>
            </el-menu-item>
          </el-submenu>
          
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-tickets"></i>
              <span>招待费用电子凭证</span>
            </template>
            <el-menu-item index="/receipt-upload">
              <i class="el-icon-upload"></i>
              <span>电子凭证采集</span>
            </el-menu-item>
            <el-menu-item index="/receipt-verify">
              <i class="el-icon-s-claim"></i>
              <span>智能票据鉴真中心</span>
            </el-menu-item>
            <el-menu-item index="/receipt-result">
              <i class="el-icon-document-checked"></i>
              <span>结果输出</span>
            </el-menu-item>
            <el-menu-item index="/receipt-history">
              <i class="el-icon-time"></i>
              <span>历史结果查询</span>
            </el-menu-item>
          </el-submenu>
          
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-s-promotion"></i>
              <span>私车公用管控</span>
            </template>
            <el-menu-item index="/car-verify">
              <i class="el-icon-s-claim"></i>
              <span>用车合规性验证</span>
            </el-menu-item>
            <el-menu-item index="/car-rule">
              <i class="el-icon-s-order"></i>
              <span>连续性检验规则</span>
            </el-menu-item>
            <el-menu-item index="/car-amount">
              <i class="el-icon-money"></i>
              <span>金额逻辑验证</span>
            </el-menu-item>
            <el-menu-item index="/car-control">
              <i class="el-icon-s-cooperation"></i>
              <span>时空中央控制</span>
            </el-menu-item>
          </el-submenu>
          
          <el-menu-item index="/statistics">
            <i class="el-icon-s-data"></i>
            <span>数据统计</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 内容区域 -->
      <el-main>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-if="$route.meta && $route.meta.title">{{ $route.meta.title }}</el-breadcrumb-item>
        </el-breadcrumb>
        
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Home',
  data() {
    return {
      activePath: ''
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    })
  },
  created() {
    this.activePath = this.$route.path
    if (!this.userInfo.name) {
      this.$store.dispatch('user/getUserInfo')
    }
  },
  watch: {
    $route(to) {
      this.activePath = to.path
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'logout') {
        this.$confirm('确认退出登录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await this.$store.dispatch('user/logout')
          this.$router.push('/login')
          this.$message.success('退出登录成功')
        }).catch(() => {})
      } else if (command === 'userInfo') {
        // 查看个人信息
      }
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #1890ff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  height: 40px;
  margin-right: 15px;
}

h1 {
  font-size: 20px;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
}

.welcome-text {
  margin-right: 20px;
}

.el-dropdown-link {
  color: white;
  cursor: pointer;
}

.el-aside {
  background-color: #304156;
}

.el-menu {
  border-right: none;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 